{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "def readfile(filep):\n",
    "    labels = [ ]\n",
    "    with open(filep,'r',encoding='utf-8') as fr:\n",
    "        for line in fr:\n",
    "            cons = line.strip().split('\\t')\n",
    "            if cons[0] in labels:\n",
    "                continue\n",
    "            else:\n",
    "                labels.append(cons[0])\n",
    "    return labels\n",
    "tra_labels = readfile('train.csv')\n",
    "tes_labels = readfile('test.csv')\n",
    "dev_labels = readfile('dev.csv')\n",
    "\n",
    "with open('class.txt','w',encoding='utf-8') as fw:\n",
    "    for line in list(set(tra_labels+tes_labels+dev_labels)):\n",
    "        fw.write(line.strip()+'\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('class.txt','r',encoding='utf-8') as  fr:\n",
    "    label2id = {}\n",
    "    i = 0 \n",
    "    for line in fr:\n",
    "        label2id[line.strip()]=i\n",
    "        i+=1\n",
    "def writeFile(filep):\n",
    "    data = []\n",
    "    fw= open(filep.replace('.csv','.txt'),'w',encoding='utf-8')\n",
    "    with open(filep,'r',encoding='utf-8') as fr:\n",
    "        for line in fr:\n",
    "            cons = line.strip().split('\\t')\n",
    "            if cons[0].strip() in label2id:\n",
    "                \n",
    "                data.append([str(label2id[cons[0].strip()]), cons[1]])\n",
    "    for d in data:\n",
    "        fw.write('\\t'.join(d)+'\\n')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "writeFile('train.csv')\n",
    "\n",
    "writeFile('dev.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "writeFile('test.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
